<?php

/**
 * @Author:      zwh
 * @DateTime:    2016-12-8 14:31:21
 * @Description: 优惠券核销
 */
class ScsCouponController extends BaseController
{
    /**
     * @Author:      zwh
     * @DateTime:    2016-12-8 14:31:41
     * @Description: 核销
     */
    public function verify(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'code' => 'required',
            )
        );
        $code=Input::get('code');
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('code'=>$code));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $coupon=Coupons::where('code',$code)->first();
            if(is_null($coupon)){
                $response = Lang::get("scs_error.coupon_notexiet_90010");
                break;
            }
            $cbi=CouponBaseInfo::find($coupon->coupon_base_info_id);
            if($cbi->type!=3){
                $response = Lang::get("scs_error.coupon_notexiet_90010");
                break;
            }
            if(in_array($coupon->status,array(1,3))){
                $response = Lang::get("scs_error.coupon_notexiet_90010");
                break;
            }
            $coupon_base_info = CouponBaseInfo::where('id',$coupon->coupon_base_info_id)->first();
            if($coupon_base_info->code_type == 1){ //随机码
                if($coupon->status==4){
                    $response = Lang::get("scs_error.coupon_error_90011");
                    $cv=CouponVerify::where('coupon_id',$coupon->id)->first();
                    $ms=MaintenanceShops::find($cv->shop_id);
                    $response['msg']='当前实物券在'.$cv->verify_time.'，在'.$ms->name.'已被核销，请与客户确认';
                    break;
                }
                $time=date('Y-m-d H:i:s',time());
                $coupon->status=4;
                $coupon->use_time=$time;
                $coupon->save();
                $coupon_verify=new CouponVerify;
                $coupon_verify->coupon_id=$coupon->id;
                $coupon_verify->shop_id=$sc_user->shop_id;
                $coupon_verify->verify_time=$time;
                $coupon_verify->verify_user=$sc_user->id;
                $coupon_verify->save();
            }else{ //统一码
                $v_coupon = Coupons::where('code',$code)->where('status',1)->first();
                $time=date('Y-m-d H:i:s',time());
                $v_coupon->status=4;
                $v_coupon->use_time=$time;
                $v_coupon->save();
                $coupon_verify=new CouponVerify;
                $coupon_verify->coupon_id=$coupon->id;
                $coupon_verify->shop_id=$sc_user->shop_id;
                $coupon_verify->verify_time=$time;
                $coupon_verify->verify_user=$sc_user->id;
                $coupon_verify->save();
            }

            try{
                if(isset($coupon)){
                    $push_couopn = CouponPush::Coupons($coupon->id,$coupon->coupon_base_info_id);
                    if(!is_null($push_couopn)){
                        $push_couopn->status = 2;
                        $push_couopn->updated_at = Common::currentTimeString();
                        $push_couopn->save();
                        Log::info("info", array("system" => "scs" ,"event" => "verify", 'input' =>Input::all() ,"output" => array('code' => 0 , 'msg' => '删除优惠券推送信息成功')));
                    }
                }
            }catch (Exception $e){
                $response = array('code' => -1 , 'msg' => '删除优惠券推送信息失败');
                Log::info("error", array("system" => "scs" ,"event" => "verify", 'input' =>Input::all() ,"output" => $e));
                break;
            }
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "verify", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-9-23 17:37:39
     * @Description: 核销前判断
     */
    public function verifyBefore(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'ids' => 'required',
            )
        );
        $ids=json_decode(Input::get('ids'),1);
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $verify_status=BusinessCommission::whereIn('id',$ids)->select('verify_status')->get()->toArray();
            $verify_status=array_column($verify_status,'verify_status');
            foreach($ids as $_id){
                if(in_array(1,$verify_status)){//如果 取消核销 报错
                    $response = Lang::get("scs_error.business_commission__error_90000");
                    $response["result"]=array("存在已核销项目");
                    break;
                }
            }
           
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "cancelVerify", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-08-21 10:25:39
     * @Description: 获取推送的优惠券(服务单)
     * @link: http://www.activity.com/v1/scs/coupon/getPushCoupon?token=442f1bcc27278986f456370d25c22366&user_id=1000037858
     */
    public function getPushCoupon(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'user_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }

            //获取参数、
            $user_id = Input::get('user_id');

            $coupon = CouponPush::Coupon($user_id);

            if(empty($coupon)){
                $response = Lang::get("scs_error.coupon_push_exist_error_120001");
                break;
            }

            //判断是否3分钟之内推送
//            $is_effective = CouponPush::ThreePush($coupon->id);
//            if($is_effective == 0){
//                $response = Lang::get("scs_error.coupon_push_time_out_error_120002");
//                break;
//            }

            //获取优惠券码
            $code = Coupons::where('id',$coupon->coupon_id)->first();
            if(empty($code)){
                $response = Lang::get("scs_error.order_coupon_error_40071");
                break;
            }

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $code);

        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getPushCoupon", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-08-14 10:54:39
     * @Description: 获取推送的优惠券（洗车单）
     * @link: http://www.activity.com/v1/scs/coupon/getPushWashCoupon?phone=15910959937
     */
    public function getPushWashCoupon(){
        $validator = Validator::make(Input::all(),
            array(
                'phone' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            //获取参数、
            $phone = Input::get('phone');

            $coupon = CouponPush::getCoupon($phone);

            if(empty($coupon)){
                $response = Lang::get("scs_error.coupon_push_exist_error_120001");
                break;
            }

            //判断是否3分钟之内推送
//            $is_effective = CouponPush::ThreePush($coupon->id);
//            if($is_effective == 0){
//                $response = Lang::get("scs_error.coupon_push_time_out_error_120002");
//                break;
//            }

            //获取优惠券码
            $code = Coupons::where('id',$coupon->coupon_id)->first();
            if(empty($code)){
                $response = Lang::get("scs_error.order_coupon_error_40071");
                break;
            }

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $code);

        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getPushWashCoupon", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}